import { _decorator, Component, Animation, Label } from 'cc';
import SensitiveFilter from "./base/SensitiveFilter";
import Tools from "./base/Tools";
import TestView from "./views/TestView";
import TestView2 from "./views/TestView2";
import BaseApp, { ViewZOrder } from "./base/BaseApp";
import GuideMgr from "./base/GuideMgr";
import GameConfig from "./base/GameConfig";
import { WebSock } from "./base/network/WebSock";
import { NetManager } from './base/network/NetManager';
import { NetConnectOptions, NetNode } from './base/network/NetNode';
import { NetData } from './base/network/NetInterface';
import { NetProtocolPako } from './base/network/NetProtocolPako';
import ComfrimView from './views/common/ComfrimView';
import { Sprite } from 'cc';
import { Button } from 'cc';
import { EventTouch } from 'cc';
import { UITransform } from 'cc';
import { Vec2 } from 'cc';
import i18n from './base/i18n/LanguageData';
import { bundleTool } from './base/bundleTool';

const { ccclass, property } = _decorator;

@ccclass("App")
export default class App extends Component {
	@property(Label) label: Label | null = null;

	@property(Sprite) spri: Sprite | null = null;

	// use this for initialization
	onLoad() {
		GameConfig.instance().loadTable();
		i18n.init("zh");
		console.log('i18n  zh:' + i18n.t('test'))
		// eval("console.log('i18n  tttttttttttttttttt');");

		BaseApp.instance().appComponent = this;
		this.label.string = SensitiveFilter.instance().filter("包含敏感词和duck和很脏的脏了");

		bundleTool.instance().loadConfig("http://192.168.0.253:8080/bundles.json").then(()=>{
			bundleTool.instance().loadBundle("Test").then((bld)=>{
				console.log("ok",bld.name);
			});
		})
		
		const callback=(dt)=> {
			this.label.string = Tools.stringFormat("{0}--{1}", "aa", "bb");
			GuideMgr.instance().setIsGuide(true);


			const net = new NetNode();

			net.setCmdListener(this.executerFunc.bind(this));
			const ws = new WebSock();
			const gp = new NetProtocolPako();

			net.init(ws, gp, null, null); // this.ExecuterFunc// 需要注册第四个参数用于数据到达处理！否则不处理
			NetManager.getInstance().setNetNode(net); // 添加指定网络频道的网络节点（频道类型：游戏。类型为1）
			const options:NetConnectOptions = {
				url: 'ws://127.0.0.1:5001/',
				autoReconnect: 0,
			};

			NetManager.getInstance().connect(options);
			const ani = this.node.getChildByPath("Sprite").getComponent(Animation);
			// ani.play();
		}

		this.scheduleOnce(callback, 1.0);

		this.node.getChildByPath("button").on("click", this.onBtnClickEvent, this);
		this.node.getChildByPath("button2").on("click", this.onBtnClickClose, this);
		this.node.getChildByPath("button3").on("click", this.onBtnClickEvent, this);
		this.node.getChildByPath("button4").on("click", this.onBtnClickEvent, this);
		this.node.getChildByPath("button5").on("click", this.onBtnClickEvent, this);
		BaseApp.instance().eventTarget.on("foobar", this.onEvent, this);
	}

	onClose() {
		BaseApp.instance().eventTarget.off("foobar", this.onEvent, this);
	}

	onEvent(message) {
		console.log("APP onEvent");
		this.label.string = message;
	}

	onClickBack(event: EventTouch) {
		const pos: Vec2 = event.getUILocation();// getLocation 会根据浏览器缩放而变化
		const sz = this.spri.getComponent(UITransform).contentSize;

		pos.x /= sz.width;
		pos.y = 1 - pos.y / sz.height;
		this.spri.material.setProperty('radius', 0.2);
		this.spri.material.setProperty('center', pos);
	}

	onBtnClickEvent(event: Button) {
		if (event.node.name == "button3") {
			BaseApp.instance().openView(TestView, "xxxx===rh23", ViewZOrder.MiddleLayer, false);
		} else if (event.node.name == "button5") {
			const data = {
				"string": "这是内容", "title": "标题x", "comfirmCall": () => {
					console.log("您按了确认");
				}, "frameType": 2
			};

			BaseApp.instance().openView(ComfrimView, data);
		} else if (event.node.name == "button4") {
			BaseApp.instance().closeAllView();
		} else {
			i18n.init("zh");
			BaseApp.instance().openView(TestView2, null, ViewZOrder.TopLayer, true, 1);
		}
	}

	onBtnClickClose(event) {
		BaseApp.instance().runScene("testScene");
	}

	executerFunc(cmd: number, buffer: NetData): void {
		// if (cmd != 1026) {
		//     console.log('收到消息: cmd = ' + cmd + `);
		// }
	}
}
